package cn.sczhyt.modules.szhy.service.impl;

import cn.sczhyt.modules.sys.dao.SysUserDao;
import cn.sczhyt.modules.sys.entity.SysUserEntity;
import cn.sczhyt.modules.szhy.entity.VO.MeetingNoticeVO;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.Query;

import cn.sczhyt.modules.szhy.dao.MeetingNoticeReceiveDao;
import cn.sczhyt.modules.szhy.entity.MeetingNoticeReceiveEntity;
import cn.sczhyt.modules.szhy.service.MeetingNoticeReceiveService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author Admin
 */
@Service("meetingNoticeReceiveService")
public class MeetingNoticeReceiveServiceImpl extends ServiceImpl<MeetingNoticeReceiveDao, MeetingNoticeReceiveEntity> implements MeetingNoticeReceiveService {


    @Autowired
    private MeetingNoticeReceiveDao receiveDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params,String userId) {

        QueryWrapper<MeetingNoticeReceiveEntity> queryWrapper = new QueryWrapper<>();
        IPage<MeetingNoticeReceiveEntity> page = this.page(
                new Query<MeetingNoticeReceiveEntity>().getPage(params),
                queryWrapper.eq("notice_receiver_id",userId)
                        //接收状态升序
                .orderByAsc("notice_receive_status")
                        //接收时间倒序
                .orderByAsc("notice_receive_datetime")

        );

        return new PageUtils(page);
    }

    /**
     * 批量添加
     * @param list
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addReceiver(String noticeReceiveIds ,String noticeId) {

        //添加之前删除所有
        QueryWrapper<MeetingNoticeReceiveEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("notice_id",noticeId);
        this.baseMapper.delete(queryWrapper);
        JSONArray jsonArray = JSONArray.fromObject(noticeReceiveIds);
        //添加

        //保存对应接收人
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                String userId = jsonArray.getJSONObject(i).getString("userId");
                String username = jsonArray.getJSONObject(i).getString("username");

                MeetingNoticeReceiveEntity receiveEntity = new MeetingNoticeReceiveEntity();
                receiveEntity.setNoticeReceiverId(userId);
                receiveEntity.setNoticeReceiverName(username);
                receiveEntity.setNoticeId(noticeId);
                receiveEntity.setNoticeReceiveStatus(false);
                this.baseMapper.insert(receiveEntity);
            }
        }
    }

    @Override
    public List<MeetingNoticeReceiveEntity> getByNoticeId(String noticeId) {

        QueryWrapper<MeetingNoticeReceiveEntity> queryWrapper = new QueryWrapper<>();

        List<MeetingNoticeReceiveEntity> list = this.baseMapper.selectList(queryWrapper.eq("notice_id", noticeId));

        return list;
    }

    @Override
    public List<MeetingNoticeVO> getList(String userId) {

        List<MeetingNoticeVO> list = receiveDao.getList(userId);
        return list;
    }

}